﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml;
using System.Xml.Linq;

namespace GoShopping.Web.ProductManager
{
    public partial class ProductManage : System.Web.UI.MasterPage
    {
        public delegate void ElementSelectedChangeHandler();
        protected void Page_Load(object sender, EventArgs e)
        {
            //SiteMap.SiteMapResolve += new SiteMapResolveEventHandler(this.ExpandForumPaths);
            if (Profile.IsAnonymous)
            {
                Response.Redirect("~/Login.aspx");
            }
            if (!IsPostBack)
            {
                if (Context.User.IsInRole("role_productmanager"))
                {
                    SetControlCommon.GetPrdtTreeView(TreeView1, Server.MapPath("~/ProductManager/Navigation.xml"), "prdt");
                    this.SiteMapPath1.Provider = SiteMap.Providers["PrdtSiteMap"];
                    string i = GetOrder() == "0" ? "0" : GetOrder();
                    string j = GetStockout() == "0" ? "0" : GetStockout();
                    hyper1.Text = "新订单:" + i + "条";
                    hyper2.Text = "缺货登记:" + j + "条";
                }
                else if (Context.User.IsInRole("role_usermanager"))
                {
                    SetControlCommon.GetPrdtTreeView(TreeView1, Server.MapPath("~/ProductManager/Navigation.xml"), "sys");
                    this.SiteMapPath1.Provider = SiteMap.Providers["SysSiteMap"];
                    this.btnProducts.Visible = false;
                    this.btnSupplier.Visible = false;
                    string i = GetAdvisory() == "0" ? "0" : GetAdvisory();
                    string j = GetEMail() == "0" ? "0" : GetEMail();
                    hyper1.Text = "未回复的咨询:" + i + "条";
                    hyper2.Text = "未发送的邮件:" + j + "条";
                }
                else if (Context.User.IsInRole("role_supermanager"))
                {
                    SetControlCommon.GetPrdtTreeView(TreeView1, Server.MapPath("~/ProductManager/Navigation.xml"), "super");
                    this.btnProducts.Visible = false;
                    this.btnSupplier.Visible = false;
                    hyper1.Visible = false;
                    hyper2.Visible = false;
                    //this.SiteMapPath1.Provider=SiteMap.Provider[];
                }
            }
            //if (ElementSelectedChange != null)
            //{

            //    ElementSelectedChange();
            //}
        }

        private string GetStockout()
        {
            BLL.PrdctInfo.StockoutBean sb = new GoShopping.BLL.PrdctInfo.StockoutBean();
            return sb.GetAll().Count.ToString();
            //throw new NotImplementedException();
        }

        private string GetOrder()
        {
            BLL.Orders.OrderBean ob = new GoShopping.BLL.Orders.OrderBean();
            return ob.GetAllOrders().Count.ToString();
            //throw new NotImplementedException();
        }

        private string GetEMail()
        {
            BLL.SysInfo.Log_EmailSendListBean log_Mail = new GoShopping.BLL.SysInfo.Log_EmailSendListBean();
            return log_Mail.GetLogMail(" AND IsSended='false'").Count.ToString();
        }

        private string GetAdvisory()
        {
            BLL.SysInfo.AdvisoryBean ab = new GoShopping.BLL.SysInfo.AdvisoryBean();
            return ab.GetAdvisoryList(" AND IsReply='false'").Count.ToString();
        }
        public TreeView MasterTreeView
        {
            get { return TreeView1; }
            set { TreeView1 = value; }
        }
        public Button PrdtButton
        {
            get { return btnProducts; }
            set { btnProducts = value; }
        }
        public Button SupplierButton
        {
            get { return btnSupplier; }
            set { btnSupplier = value; }
        }
        public HyperLink HperLink1
        {
            get { return hyper1; }
            set { hyper1 = value; }
        }
        public HyperLink HperLink2
        {
            get { return hyper2; }
            set { hyper2 = value; }
        }
        public ElementSelectedChangeHandler ElementSelectedChange
        {
            private get;
            set;
        }
        protected void btnSupplier_Click(object sender, EventArgs e)
        {
            Response.Redirect("OrderList.aspx");
        }

        protected void btnProducts_Click(object sender, EventArgs e)
        {
            Response.Redirect("ProductsList.aspx");
        }
    }
}
